package org.example.juc.question;

import java.util.concurrent.atomic.AtomicInteger;

//1114. 按序打印
public class PrintInOrderSolution {
    private AtomicInteger jobDone = new AtomicInteger(0);

    public PrintInOrderSolution() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        jobDone.set(1);
    }

    public void second(Runnable printSecond) throws InterruptedException {

        while (jobDone.get() != 1) {

        }
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        jobDone.set(2);
    }

    public void third(Runnable printThird) throws InterruptedException {

        while (jobDone.get() != 2) {

        }
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}
